# game constants
from constants import *

# cocos componets used by this scene
from cocos.director import director
from cocos.scene import Scene
from cocos.layer import Layer, ColorLayer
from cocos.scenes.transitions import *
from cocos.actions import *
from cocos.sprite import Sprite
from cocos.text import Label

# pyglet  componets used by this scene
import pyglet
from pyglet import gl, font
from pyglet.window import key


class GenericLayer(Layer):

    is_event_handler = True     #: enable pyglet's events

    def __init__( self, interface ):
        # call the parent's constructor
        super(GenericLayer, self).__init__()

        # keep track of the interface
        self.interface = interface
        # (optional) keep track of other components that are accessed frequently
        # instead of doing every time  self.interface.game.galaxy -> self.galaxy  :)
        # self.galaxy = self.interface.game.galaxy
        # etc...
        
        # just a uniform color background
        self.add(ColorLayer(120,40,40,200), z=-1)
        
        # add a label
        self.infoLabel = Label( "Generic Layer\nthis is a multiline label",
            font_size=12,
            multiline=True,
            height=100,
            width=100,
            x=5,
            y=SCREEN_HEIGHT - 10,
            anchor_x=font.Text.LEFT,
            anchor_y=font.Text.TOP )
        self.add( self.infoLabel )
        
        
    def on_enter(self):
        super( GenericLayer, self ).on_enter()   
        
        # shedule a function to update itself (the graphic) every frame
        self.schedule(self.update)     
        
        # shedule a function to update the game state every x seconds
        self.schedule_interval(self.updateGame, 4) 


    def on_exit(self):
        super( GenericLayer, self ).on_exit()   
        
        # remember to unschedule each function previously schduled
        self.unschedule(self.update) 
        self.unschedule(self.updateGame) 
        
        
    def update(self,dt, *args, **kwargs):
        print "Updating scene..."
        
        
    def updateGame(self,dt, *args, **kwargs):
        print "Updating game..."
        self.interface.game.updateGalaxy()
        self.interface.game.updateAI()


    def on_key_press( self, k , m ):
        print "Code of pressed key: " + str(k)
        return pyglet.event.EVENT_HANDLED
               

# construct the scene here and return
# instead of doing this in the graphic interface (for example self.genericScene = Scene (GenericLayer()) )
# this is because here one can define more complicated scenes, independent of the interface
def GenericScene(interface):
    # in this generic example, build a scene composed by only one layer
    return Scene( GenericLayer(interface) )




